package com.woniu.p2p.behindServiceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.p2p.behindService.BehindCouponService;
import com.woniu.p2p.entity.Coupon;
import com.woniu.p2p.mapper.CouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author 86152
 * @create 2020/8/12
 * @since 1.0.0
 */
@Service
public class BehindCouponServiceImp implements BehindCouponService {

    @Autowired
    CouponMapper couponMapper;
    /*条件查询*/
    @Override
    public PageInfo<Coupon> getcouponbycons(String status, String keyword, Date starttime, Date endtime, Integer currentPage, int pagesize) {
        PageHelper.startPage(currentPage,pagesize);
        List<Coupon> couponPageInfo=couponMapper.getcouponbycons(status,keyword,starttime,endtime);
        PageInfo<Coupon> couponPageInfo1 = new PageInfo<>(couponPageInfo);
        return couponPageInfo1;
    }
    /*根据id删除优惠券并重新查询*/
    @Override
    @Transactional
    public PageInfo<Coupon> deletebyid(String status, String keyword, Date starttime, Date endtime, Integer currentPage, int pagesize, Integer couponId) {
        couponMapper.deleteByPrimaryKey(couponId);
        PageHelper.startPage(currentPage,pagesize);
        List<Coupon> couponPageInfo=couponMapper.getcouponbycons(status,keyword,starttime,endtime);
        PageInfo<Coupon> couponPageInfo1 = new PageInfo<>(couponPageInfo);
        return couponPageInfo1;
    }
    /*根据id查询优惠券*/
    @Override
    public Coupon findcouponbyid(Integer couponId) {
        Coupon coupon = couponMapper.selectByPrimaryKey(couponId);
        return coupon;
    }
    /*添加优惠券*/
    @Override
    public int addcoupon(Coupon coupon) {
        return couponMapper.insert(coupon);
    }
    /*批量删除*/
    @Override
    @Transactional
    public int piliangdel(int[] ids) {
        int row=0;
        for(int id:ids){
            row+=couponMapper.deleteByPrimaryKey(id);
        }
        return row;
    }
    /*查询所有优惠券*/
    @Override
    public List<Coupon> selectAll() {
        return couponMapper.selectAll();
    }
}
